package leetcode3;

public class Day20 {

    /**
     * 链表求和
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1);
        int tmp=0;
        ListNode cur = head;
        while(l1!=null || l2!=null) {
            if(l1!=null) {
                tmp += l1.val;
                l1 = l1.next;
            }
            if(l2!=null) {
                tmp += l2.val;
                l2 = l2.next;
            }
            ListNode t = new ListNode(tmp%10);
            tmp/=10;
            cur.next = t;
            cur = cur.next;
        }
        if(tmp>0 ){
            ListNode t = new ListNode(tmp%10);
            tmp/=10;
            cur.next = t;
        }
        return head.next;
    }


    /**
     * 颜色分类
     * @param nums
     */
    public void sortColors(int[] nums) {
        int n = nums.length;
        int left=-1,right=n,i=0;
        while(i<right) {
            if(nums[i]==0) {
                swap(nums,i++,++left);
            }else if(nums[i]==1) {
                i++;
            }else {
                swap(nums,i,--right);
            }
        }
    }
    public void swap(int[] nums,int i,int j) {
        int tmp = nums[i];
        nums[i]=nums[j];
        nums[j]=tmp;
    }
}
